#FUNCTION FOR SENSITITVY ANALYSIS
###LM RESULTS
s_ci <- function(the.coef,#coef of interest
                   the.se, #standard error of coef
    gamma=c(0, .01, .05, .1, .2, .3),  #effect of latent factor on Y
    delta= .4, # prevelence diffrence in U (latent unmeasured factor) b/w treatment and control
    gamma.glm = seq(0.5, 1.5, 0.1),
    pi.1=.7,
    pi.0=.3,
    mod_type = c("lm", "glm")
                      ) {

    if (mod_type == "lm"){
        ###SENSITIVITY
        the.bias <- delta * gamma
        corrected_beta <- the.coef - the.bias
        corrected_lb   <- (the.coef - 1.96 * the.se) - the.bias
        corrected_ub   <- (the.coef + 1.96 * the.se) - the.bias

      return(data.frame(gamma, corrected_beta, corrected_lb, corrected_ub))
    }
    #GLM RESULTS
    if (mod_type == "glm"){
        odds.ratio <- exp(the.coef) #the estimated effect, expressed as odds ratio
        odds.ratio.lb <- exp(the.coef - 1.96 * the.se)
        odds.ratio.ub <- exp(the.coef + 1.96 * the.se)


        gamma <- gamma.glm


      ###SENSITIVITY
        the.bias <- (1 + (gamma - 1) * pi.1) / (1 + (gamma - 1) * pi.0)
        corrected.n.t <- odds.ratio / the.bias
        corrected.n.t.lb <-  odds.ratio.lb / the.bias
        corrected.n.t.ub <- odds.ratio.ub / the.bias
        return(data.frame(gamma, corrected.n.t, corrected.n.t.lb, corrected.n.t.ub))
    }
    if (!(mod_type %in% c("lm", "glm"))){
          stop("mod_type must be 'lm' or 'glm'")
    }


    }



